﻿#include "ui_lvgl.h"

#define HOME_COL_W (LCD_W/4+5)
#define HOME_COL_H (LCD_W/4+5)

void lv_event_cpu_btn_cb_t(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e); // 获取事件类型
    if (code == LV_EVENT_CLICKED) { // 如果是点击事件
        if (cont != NULL) {
            lv_obj_del(cont);
            cont = NULL;
        }
        printf("Exit Home UI!\n");
        menu = MENU_CPU;
    }
}

void home_show(void)
{
    printf("goto Home UI!\n");

    if (cont != NULL) {
        printf("%s err\n", __func__);
        cont = NULL;
    }

    cont = lv_obj_create(lv_scr_act()); // 创建容器
    lv_obj_set_size(cont, LCD_H, LCD_W); // 设置容器大小
    lv_obj_set_layout(cont, LV_LAYOUT_GRID); // 设置 Grid 布局

    // 定义网格模板
    static lv_coord_t col_dsc[] = { HOME_COL_W, HOME_COL_W, HOME_COL_W, HOME_COL_W, LV_GRID_TEMPLATE_LAST }; // 4 列，每列 70px
    static lv_coord_t row_dsc[] = { HOME_COL_H, HOME_COL_H, HOME_COL_H, LV_GRID_TEMPLATE_LAST }; // 3 行，每行 50px
    lv_obj_set_grid_dsc_array(cont, col_dsc, row_dsc);

    // 添加子对象并放置到网格中
    lv_obj_t* btn1 = lv_btn_create(cont);
    lv_obj_set_grid_cell(btn1, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_STRETCH, 0, 1); // 放置到第 1 行第 1 列
    lv_obj_t* lb_btn1 = lv_label_create(btn1);
    lv_obj_align(lb_btn1, LV_ALIGN_CENTER, 0, 0);
    lv_label_set_text(lb_btn1, "LED");


    lv_obj_t* btn2 = lv_btn_create(cont);
    lv_obj_set_grid_cell(btn2, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_STRETCH, 0, 1); // 放置到第 1 行第 2 列
    lv_obj_t* lb_btn2 = lv_label_create(btn2);
    lv_obj_align(lb_btn2, LV_ALIGN_CENTER, 0, 0);
    lv_label_set_text(lb_btn2, "CPU");
    lv_obj_add_event_cb(btn2, lv_event_cpu_btn_cb_t, LV_EVENT_ALL, NULL);

    lv_obj_t* btn3 = lv_btn_create(cont);
    lv_obj_set_grid_cell(btn3, LV_GRID_ALIGN_STRETCH, 2, 1, LV_GRID_ALIGN_STRETCH, 0, 1); // 放置到第 2 行第 1 列
    lv_obj_t* lb_btn3 = lv_label_create(btn3);
    lv_obj_align(lb_btn3, LV_ALIGN_CENTER, 0, 0);
    lv_label_set_text(lb_btn3, "TIME");

    lv_obj_t* btn4 = lv_btn_create(cont);
    lv_obj_set_grid_cell(btn4, LV_GRID_ALIGN_STRETCH, 3, 1, LV_GRID_ALIGN_STRETCH, 0, 1); // 放置到第 1 行第 1 列

    lv_obj_t* btn5 = lv_btn_create(cont);
    lv_obj_set_grid_cell(btn5, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_STRETCH, 1, 1); // 放置到第 1 行第 2 列

    lv_obj_t* btn6 = lv_btn_create(cont);
    lv_obj_set_grid_cell(btn6, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_STRETCH, 1, 1); // 放置到第 2 行第 1 列

    lv_obj_t* btn7 = lv_btn_create(cont);
    lv_obj_set_grid_cell(btn7, LV_GRID_ALIGN_STRETCH, 2, 2, LV_GRID_ALIGN_STRETCH, 1, 1); // 放置到第 1 行第 1 列

    lv_obj_t* btn8 = lv_btn_create(cont);
    lv_obj_set_grid_cell(btn8, LV_GRID_ALIGN_STRETCH, 0, 2, LV_GRID_ALIGN_STRETCH, 2, 1); // 放置到第 1 行第 2 列

    lv_obj_t* btn9 = lv_btn_create(cont);
    lv_obj_set_grid_cell(btn9, LV_GRID_ALIGN_STRETCH, 2, 2, LV_GRID_ALIGN_STRETCH, 2, 1); // 放置到第 2 行第 1 列    

}
